import requests
from lxml import etree
from application import db
from sqlalchemy import text


class JobTask():
    def que(self):
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"
        }
        sql = text("select BV from bilibili")
        result = db.engine.execute(sql)
        for i in result:
            a = requests.get(url="https://www.bilibili.com/video/{}".format(i[0]), headers=headers)
            a.encoding = "utf-8"
            tree = etree.HTML(a.text)
            title = tree.xpath("//html/head/title/text()")[0]
            if "出错啦" in title or "视频去哪了呢" in title:
                print("视频没了~~~~~~~~~~~~~~~~~~~~~")
                print(i[0])
                delete_sql = "delete from bilibili where BV='{}'".format(i[0])
                try:
                    db.engine.execute(delete_sql)
                except:
                    print("delete_sql错误" + title + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

            else:
                print(title)
        pass

    def run(self,params):
        self.que()

        pass
